<?php

namespace app\service;

use Illuminate\Database\Capsule\Manager as DB;

class config
{
    public static function get($name = '', $platform = '')
    {
        // 1基本信息 2支付宝 3微信 4CDN 5短信 6APP推送 7订单立减 8分享
        $config = DB::table('sys_config')->get();
        switch ($name) {
            case 'addons':
                return self::processAddons($config);
            case 'chat':
                return self::processChat($config);
            case 'payment':
                return self::processPayment($config, $platform);
            case 'wechat':
                return self::processWechat($config);
            case 'alipay':
                return self::processAlipay($config);
            case 'joinpay':
                return self::processJoinpay($config);
            case 'wxOfficialAccount':
            case 'wxMiniProgram':
            case 'App':
            case 'H5':
                return self::processPlatform($config, $name);
            case 'share':
                return self::processShare($config);
            case 'shop':
                return self::processShop($config);
            case 'store':
                return self::processStore($config);
            case 'order':
                return self::processOrder($config);
            case 'sms':
                return self::processSms($config);
            case 'express':
                return self::processExpress($config);
            case 'commission':
                return self::processCommission($config);
            default:
                if (empty($name)) {
                    $result = [
                        'addons' => self::processAddons($config),   // 插件
                        'chat' => self::processChat($config),       // 客服
                        'payment' => self::processPayment($config, $platform), // 支付方式
                        'share' => self::processShare($config),     // 分享
                        'shop' => self::processShop($config),       // 商城
                        'store' => self::processStore($config),     // 门店
                        'commission' => self::processCommission($config), // 分佣配置
                    ];
                    if (in_array($platform, ['wxOfficialAccount', 'wxMiniProgram'])) {
                        $result['wechat'] = self::processWechat($config); // 微信支付
                    }
                } else {
                    foreach ($config as $config_item) {
                        if ($config_item->key == $name) {
                            $result = $config_item->value;
                            break;
                        }
                    }
                }
                return $result ?? '';
        }
    }

    private static function processAddons($config)
    {
        return $data ?? [];
    }

    private static function processChat($config)
    {
        return $data ?? [];
    }

    private static function processPayment($config, $platform)
    {
        if ($platform == 'wxOfficialAccount') {
            $platform = '微信公众号';
        }
        if ($platform == 'wxMiniProgram') {
            $platform = '小程序';
        }
        $data = [];
        foreach ($config as $config_item) {
            if ($config_item->type == 2 && $config_item->key == 'platform') {
                $platform_arr = json_decode($config_item->value, true);
                if (in_array($platform, $platform_arr)) {
                    array_push($data, 'alipay');
                }
            }
            if ($config_item->type == 3 && $config_item->key == 'platform') {
                $platform_arr = json_decode($config_item->value, true);
                if (in_array($platform, $platform_arr)) {
                    array_push($data, 'wechat');
                }
            }
            if ($config_item->type == 16 && $config_item->key == 'platform') {
                $platform_arr = json_decode($config_item->value, true);
                if (in_array($platform, $platform_arr)) {
                    array_push($data, 'wallet');
                }
            }
            if ($config_item->type == 30 && $config_item->key == 'platform') {
                $platform_arr = json_decode($config_item->value, true);
                if (in_array($platform, $platform_arr)) {
                    array_push($data, 'joinpay');
                }
            }
        }
        return $data;
    }

    private static function processWechat($config)
    {
        foreach ($config as $config_item) {
            if ($config_item->type == 3 || $config_item->type == 12) {
                $data[$config_item->key] = $config_item->value;
            }
        }
        $data['appid'] = $data['app_id'] ?? '';
        return $data ?? [];
    }

    private static function processAlipay($config)
    {
        foreach ($config as $config_item) {
            if ($config_item->type == 2) {
                $data[$config_item->key] = $config_item->value;
            }
        }
        return $data ?? [];
    }

    private static function processJoinpay($config)
    {
        foreach ($config as $config_item) {
            if ($config_item->type == 30) {
                $data[$config_item->key] = $config_item->value;
            }
        }
        return $data ?? [];
    }

    private static function processPlatform($config, $name)
    {
        switch ($name) {
            case 'wxOfficialAccount':
                $type = 12;
                break;
            case 'wxMiniProgram':
                $type = 13;
                break;
            case 'H5':
                $type = 14;
                break;
            case 'App':
                $type = 15;
                break;
            default:
                break;
        }
        foreach ($config as $config_item) {
            if ($config_item->type == $type) {
                $data[$config_item->key] = $config_item->value;
            }
        }
        return $data ?? [];
    }

    private static function processShare($config)
    {
        foreach ($config as $config_item) {
            // 分享
            if ($config_item->type == 8) {
                $data[$config_item->key] = $config_item->value;
            }
        }
        $data['user_poster_bg'] = json_decode($data['user_poster_bg'] ?? '[]');
        return $data;
    }

    private static function processShop($config)
    {
        foreach ($config as $config_item) {
            if ($config_item->type == 1) {
                $data[$config_item->key] = $config_item->value;
            }
        }
        $data['copyright'] = explode(',', $data['copyright'] ?? ',');
        return $data;
    }

    private static function processStore($config)
    {
        return $data ?? [];
    }

    private static function processOrder($config)
    {
        foreach ($config as $config_item) {
            if ($config_item->type == 11) {
                $data[$config_item->key] = $config_item->value;
            }
        }
        return $data ?? [];
    }

    private static function processExpress($config)
    {
        foreach ($config as $config_item) {
            if ($config_item->type == 17) {
                $data[$config_item->key] = $config_item->value;
            }
        }
        return $data ?? [];
    }

    private static function processCommission($config)
    {
        foreach ($config as $config_item) {
            if ($config_item->type == 19) {
                $data[$config_item->key] = $config_item->value;
            }
        }
        return $data ?? [];
    }

    private static function processSms($config)
    {
        foreach ($config as $config_item) {
            if ($config_item->type == 5) {
                $data[$config_item->key] = $config_item->value;
            }
        }
        if (!empty($data['status_hywx'])) {
            return ['type' => 'hywx', 'account' => $data['hywx_appid'] ?? '', 'password' => $data['hywx_secret'] ?? ''];
        }
        if (!empty($data['status_dxb'])) {
            return [
                'type' => 'dxb',
                'username' => $data['dxb_appid'] ?? '',
                'password' => $data['dxb_secret'] ?? '',
                'sign' => $data['dxb_sign'] ?? '',
            ];
        }
        if (!empty($data['status_aliyun'])) {
            return [
                'type' => 'aliyun',
                'AccessKeyId' => $data['aliyun_appid'] ?? '',
                'secret' => $data['aliyun_secret'] ?? '',
                'SignName' => $data['aliyun_sign'] ?? '',
                'TemplateCode' => $data['aliyun_template'] ?? '',
            ];
        }
        return null;
    }
}

